<template>
	<view class="mainPage u-skeleton" :style="{'padding-bottom': `${$store.state.safeArea.bottom || 0}px`}">
		<view class="box1">
			<view class="doctor-info">
				<image class="touxiang" :src="user_Info.user_avatar" mode="aspectFill"></image>
				<text class="name">{{user_Info.user_name}}</text>
			</view>
			<view class="hezi u-skeleton-fillet">
				<view class="item">
					<view class="num">
						{{workInfo.service}}
					</view>
					<view class="text">
						服务人数
					</view>
				</view>
				<view class="item">
					<navigator hover-class="none" url="../../pagesA/workbench/petReview">
						<view class="num">
							{{workInfo.satisfied}}%
						</view>
						<view class="text">
							满意率
						</view>
					</navigator>
				</view>
				<view class="item">
					<navigator hover-class="none" url="../../pagesA/workbench/fansList">
						<view class="num">
							{{workInfo.fans}}
						</view>
						<view class="text">
							粉丝
						</view>
					</navigator>
				</view>
			</view>
		</view>
		<!-- <u-skeleton :loading="loading" :animation="true" bgColor="#FFF" ></u-skeleton> -->
		<view class="box2">
			<view class="item">
				<navigator hover-class="none" url="../../pagesA/workbench/myInquiry">
					<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/wodewenzhen.png" mode=""></image>
					<text>我的问诊</text>
				</navigator>
			</view>
			<view class="item">
				<navigator hover-class="none" url="../../pagesA/workbench/bookingManagement">
					<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/yuyueguanli.png" mode=""></image>
					<text>预约管理</text>
				</navigator>
			</view>
			<view class="item">
				<navigator hover-class="none" url="../../pagesA/workbench/articleCenters">
					<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/wenzhangzhongxin.png" mode=""></image>
					<text>文章中心</text>
				</navigator>
			</view>
			<view class="" style="display: flex;margin: 0 23rpx;flex-direction: column;align-items: center;">
				<button open-type="contact" type="default" style="border:none !important;outline:none !important; width: 76rpx;height: 76rpx;background: url(https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/yishengzhushou.png) no-repeat ;background-size: 100% 100%;font-size: 28rpx;margin-left: 0;margin-right: 0;padding-left: 0;padding-right: 0;color: #333333;margin-top: 16rpx;" show-message-card session-from send-message-path send-message-title></button>
				<!-- <image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/contact.png"></image> -->
				<button open-type="contact" type="default" style="border:none !important;outline:none !important; background-color:transparent !important;font-size: 28rpx;margin-left: 0;margin-right: 0;padding-left: 0;padding-right: calc(100% - 260rpx);color: #333333;margin: 0;" show-message-card session-from send-message-path send-message-title>医生助手</button>
			</view>
			<!-- <view class="item">
				<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/yishengzhushou.png" mode=""></image>
				<text>医生助手</text>
			</view> -->
		</view>
		<view class="" style="margin-bottom: 14rpx;background: #fff;" v-if="is_qd == 0">
			<view class="fsb lrmg-30 tmpadd-20" style="border-bottom: 1rpx solid #F0F0F0;">
				<view class="f32 flex aic" style="font-weight: 600;color: #1E1E1E;" @click="qiangdanss()">
					<view class="">抢单中心</view>
					<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/img/shuaxin.png" style="width:28rpx;height:28rpx" class="mg-l-10"></image>
				</view>
				<view class="flex aic" style="color: #909090;" @click="tiaozhuans">
					<view class="mg-r-10">更多</view>
					<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/icon_enter_homepage.png" style="width:12rpx;height:20rpx" class=""></image>
				</view>
			</view>
			<view class="" style="margin: 20rpx 30rpx 0;padding-bottom: 20rpx;" v-if="OrderInfo != ''">
				<view class="flex jsb">
					<view class="flex">
						<image :src="OrderInfo.user_avatar" style="width:64rpx;height:64rpx" class="bra50"></image>
						<view class="mg-l-10 mg-t-10">
							<view class="f30" style="color: #323232;font-weight: 500;">{{OrderInfo.user_nickname}}</view>
							<view class="f26 bold400 mg-t-10" style="color: #909090;">{{OrderInfo.petfiles_race == 0 ? '其他' : (OrderInfo.petfiles_race == 1 ? '猫' : '狗')}} {{OrderInfo.petfiles_sex == 0 ? '母' : '公'}} {{OrderInfo.petfiles_age}}</view>
						</view>
					</view>
					<view class="fcc bra60" style="border: 1rpx solid #FA6400;width: 160rpx;height: 56rpx;color: #FA6400;">
						<view class="">{{OrderInfo.consultation_type == 1 ? '图文问诊' : '电话问诊'}}</view>
					</view>
				</view>
				<view class="flex mg-t-10">
					<view class="" style="width: 74rpx;height:1rpx;"></view>
					<view class="bold400" style="color: #323232;width: calc(100% - 74rpx);line-height: 45rpx;">{{OrderInfo.describe}}</view>
				</view>
			</view>
			<view class="" style="background: #FAFAFA;" v-if="OrderInfo != ''">
				<view class="lrmg-30 tmpadd-20 fsb" style="">
					<view class="bold400" style="color: #FA6400;margin-left: 74rpx;">¥{{OrderInfo.discount_price}}<text style="color: #909090;margin-left: 10rpx;">{{OrderInfo.time_str}}</text></view>
					<view class="fcc bra60 f30" style="width: 144rpx;height: 64rpx;background: linear-gradient(270deg, #FFC347 0%, #FF9331 100%);" @click="submit(OrderInfo.order_id)">
						<view class="fff">抢单</view>
					</view>
				</view>
			</view>
			<view class="fdc aic jc-center" style="width: 100%;height: 300rpx;" v-if="OrderInfo == ''">
				<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/img/zanwu.png" style="width:162rpx;height:120rpx" class=""></image>
				<view class="f32 mg-t-10" style="color: #909090;">暂无问诊订单</view>
			</view>
		</view>
		<view class="box3">
			<view class="title">
				待处理
			</view>
			<view class="list" v-if="workInfo.petfiles">
				<view class="item" v-for="(item,index) in workInfo.petfiles" :key="index" @click="next(item)">
					<view class="first-line">
						<view class="left">
							<view class="touxiang-box">
								<image class="img" :src="item.avatar" mode=""></image>
								<text class="shu" v-if="item.read_num != 0">{{item.read_num}}</text>
							</view>
							<view class="name">
								{{item.nickname}}
							</view>
							<view class="pet-info">
								{{item.race.slice(0,2)}} {{item.sex}} {{item.weight}}
							</view>
						</view>
						<view class="right">
							<view class="wenzhenleixing">
								{{item.consultation_type}}
							</view>
							<view class="state">
								{{item.status == 0 ? '待接诊' : '进行中'}}
							</view>
						</view>
					</view>
					<view class="second-line">
						<view class="wenti">
							{{item.describe}}
						</view>
						<view class="time">
							{{item.en_time}}
						</view>
					</view>
				</view>
			</view>
			<view class="zanwuyuyue" v-else>
				<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/zanwu.png" mode=""></image>
				<text>目前暂无接诊</text>
			</view>
		</view>
	</view>
</template>

<script>
	var that
	var audio
	export default {
		data() {
			return {
				user_Info:{},
				workInfo:{service:'-',satisfied:'-',fans:'-'},
				flag:0,
				audio:'',
				OrderInfo:{"order_id":"","discount_price":"0.00","consultation_type":1,"describe":"","en_time":"","user_id":'',"pet_id":'',"user_nickname":"","user_avatar":"","petfiles_race":'',"petfiles_sex":'',"petfiles_age":"","time_str":""},
				is_qd:1
			}
		},
		onLoad() {
			// console.log(this.$store.state.data)
			that = this
			audio = uni.createInnerAudioContext()
			audio.src = 'https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng/ding.mp3'
			// 判断是否有缓存
			let userInfo = uni.getStorageSync("chongdoc_login")
			if (userInfo == '' || userInfo == undefined) {
				uni.reLaunch({
					url:'../login/login'
				})
			} else {
				console.log('已登录')
				that.tiaozhuan()
			}
		},
		onShow() {
			that.$store.state.doc_type = 1
			that.user_Info = that.$store.state.user_Info
			// 判断是否有缓存
			let userInfo = uni.getStorageSync("chongdoc_login")
			if (userInfo == '' || userInfo == undefined) {
			} else {
				// console.log('已登录')
				if(that.flag == 1){
					that.requestData()
					if(that.is_qd == 0){
						that.qiangdan()
					}
				}
			}
		},
		methods: {
			
			submit(id){
				uni.showLoading({
					mask: true,
					title: '正在抢单...'
				});
				let postData = {
					order_sn:id
				}
				that.$postAjax1('Doc_QianDan_grabbingOrders',postData,function(data){
					uni.hideLoading()
					if(data.data.code == 1){
						that.qiangdan()
						that.requestData()
					}else if(data.data.code == 50005){
						uni.showToast({
							icon:'none',
							title:data.data.msg 
						})
						that.qiangdan()
					}
				})
			},
			tiaozhuans(){
				if(that.OrderInfo == ''){
					uni.showToast({
						title: '请刷新订单',
						icon: 'none',
						duration: 1500
					});
					return
				}
				uni.navigateTo({
					url:'../../pagesB/mykehu/grabbingorders'
				})
			},
			jt(){
				// 首页开启监听
				var onMessageReceiveds
				onMessageReceiveds = function(event) {
					console.log(that.$store.state.doc_type);
					if(that.$store.state.doc_type == 1){
						console.log('医生端首页监听' ,event);
						audio.play()
						that.new_xiaoxi(event.data)
					}
				};
				that.$tim.on(that.$TIM.EVENT.MESSAGE_RECEIVED, onMessageReceiveds);
			},
			//受到新消息处理
			new_xiaoxi(data){
				data = data[0]
				let list = that.workInfo.petfiles
				let TTinfo = JSON.parse(data.payload.data)
				if(list.length == 0){
					that.requestData()
					return
				}
				setTimeout(function(){
					for (var i = 0; i < list.length; i++) {
						if(list[i].order_id == TTinfo.MsgContent.relation){
							list[i].read_num = list[i].read_num + 1
							let temp = JSON.stringify(list[i])
							let temps = JSON.parse(temp)
							list.splice(i,1)
							list.unshift(temps)
							uni.showTabBarRedDot({
								index:0
							})
							break
						}else{
							if(i == list.length - 1){
								that.requestData()
							}
						}
					}
				},500)//处理异步问题
			},
			next(item){
				uni.navigateTo({
					url:'../../pagesB/myclient/chatWindow?item=' + JSON.stringify(item)
				})
			},
			tiaozhuan(){
				// status  0刚注册进来，1审核通过，2审核被拒绝，-1审核中
					uni.showLoading({
						mask: true,
						title: '加载中...'
					});
					that.$postAjax1('Doctor_doctorInfo','',function(data){
						uni.hideLoading()
						data = data.data.data
						that.$store.state.user_Info = data
						that.user_Info = data
						if(data.status == 0){
							uni.reLaunch({
								url:'../register/authentication?type=1'
							})
							return
						}else if(data.status == 1){
							that.$postAjax1('Doc_SendChat_get_user_sign','',function(data){
								//登录后要请求的都写这个里面
								that.$store.state.im_id = data.data.data.user_im_id
								that.login(data.data.data)
							})
						}else if(data.status == 2){
							uni.reLaunch({
								url:'../register/authentication?type=2'
							})
							return
						}else if(data.status == -1){
							uni.reLaunch({
								url:'../register/examine'
							})
							return
						}
					})
			},
			dingwei(){
				 uni.getLocation({
					 type:'gcj02',
					 success(res){
						 that.get_ad(res)
					 }
				 })
			},
			// 获取adcode
			async get_ad(item){
				let adcode = await that.get_s(item)
				let postData = {
					longitude:item.longitude,
					latitude:item.latitude,
					city_code:adcode
				}
				that.$postAjax1('Doctor_set_location',postData,function(data){
					
				})
			},
			get_s(item){
				let a = ''
				let url = 'https://restapi.amap.com/v3/geocode/regeo?output=JSON&location='+item.longitude+','+item.latitude+'&key=3700876129d97a1cc45ac2b7725f8a24&radius=1000&extensions=all'
				// 1.2 获取用户位置成功后，将会返回 latitude, longitude 两个字段，代表用户的经纬度位置
				console.log(item)
				return new Promise((resolve, reject) => {
					uni.request({
						url:url,
						async: false,
						dataType: 'json',
						method: 'GET',
						success(res){
							console.log(res,'获取城市code成功')
							resolve(res.data.regeocode.addressComponent.adcode)
						},
						fail(res){
							console.log(res,'获取城市code失败')
							that.get_s(item)
						}
					})
				});
			},
			login(item){
				//登录IM
				let promise = that.$tim.login({userID: item.user_im_id, userSig: item.sig});
				promise.then(function(imResponse) {
				  console.log('登陆成功----' + JSON.stringify(imResponse.data)); // 登录成功
				  that.jt()
				  setTimeout(function(){
					  that.flag = 1
				  },500)
				  that.requestData()
				  that.dingwei()
				  that.qiangdans()
				  if (imResponse.data.repeatLogin === true) {
				    console.log('标识账号已登录，本次登录操作为重复登录。----'+JSON.stringify(imResponse.data.errorInfo));
				  }
				}).catch(function(imError) {
				  console.warn('登录失败的相关信息------', imError); // 登录失败的相关信息
				});
			},
			qiangdan(){
				that.$postAjax1('Doc_QianDan_getOrderInfo','',function(data){
					console.log(data,555);
					that.OrderInfo = data.data.data
				})
			},
			qiangdans(){
				that.$postAjax1('Doc_QianDan_getQDConfig','',function(data){
					// console.log(data.data.data.dor_status,555);
					that.is_qd = data.data.data.dor_status
					if(that.is_qd == 0){
						that.qiangdan()
					}
				})
			},
			qiangdanss(){
				uni.showLoading({
					mask: true,
					title: '加载中...'
				});
				that.$postAjax1('Doc_QianDan_getOrderInfo','',function(data){
					uni.hideLoading()
					console.log(data,555);
					that.OrderInfo = data.data.data
				})
			},
			createPromise() {
			        var promise;
			        promise = new Promise(function(resolve, reject) {
			            var elapse = Math.random() * 2000;
			            setTimeout(resolve, elapse);
			        });
			        return promise;
			
			
			 },
			 getOutInfo(list,suc){ 
					let promise = this.$tim.getConversationProfile('C2Cuser_' + list.userid)
					promise.then(function(imResponse) {
							// console.log(imResponse.data.conversation);//Object.assign(det, postData);
							suc(imResponse.data.conversation.unreadCount)
					}).catch((imError)=> {
						console.log('getConversationProfile error:', imError); // 获取会话资料失败的相关信息
						// reject(imError)
					});
			},
			requestData(){  //https://ask.suoweilai.com/Doctor_get_doctor_data   医生端 - 获取工作台医生所有数据
				var that = this
				that.$postAjax1('Doctor_get_doctor_data','',function(data){
					console.log(data.data.data,'医生端待处理')
					data = data.data.data
					that.workInfo=data
					if(that.workInfo == ''){
						uni.removeTabBarBadge({
							index: 0
						})
						return
					}
					if(that.workInfo.petfiles == undefined){
						return
					}
					for (var i = 0; i < that.workInfo.petfiles.length; i++) {
						if(that.workInfo.petfiles[i].read_num != 0){
							// console.log(i,'进来了');
							uni.showTabBarRedDot({
								index:0
							})
							break
						}else{
							if(i == that.workInfo.petfiles.length - 1){
								uni.removeTabBarBadge({
									index:0
								})
							}
						}
					}
				})
			}
		},
	}
</script>

<style scoped lang="scss">
button::after{ border: none; }
.box1{
	position: relative;
	width: 100%;
	height: 294rpx;
	background-image:url(https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/img/kongzhitaibeijing.png) ;
	background-repeat: no-repeat;
	background-size: cover;
	margin-bottom: 98rpx;
	.doctor-info{
		padding: 114rpx 60rpx;
		.touxiang{
			display: inline-block;
			width: 88rpx;
			height: 88rpx;
			border-radius: 50%;
			box-shadow: 0rpx 4rpx 8rpx 0rpx #FE7F0F;
			vertical-align: middle;
			margin-right: 24rpx;
		}
		.name{
			font-size: 28rpx;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 44rpx;
			vertical-align: middle;
		}
	}
	.hezi{
		position: absolute;
		height: 132rpx;
		padding: 24rpx 0 20rpx;
		bottom: -78rpx;
		left: 32rpx;
		width: calc(100% - 64rpx);
		background: #FFFFFF;
		box-shadow: 0rpx 4rpx 8rpx 0rpx #ECE4E4;
		border-radius: 12rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		.item{
			margin: 0 55rpx;
			text-align: center;
			.num{
				font-size: 28rpx;
				font-weight: 500;
				color: #FF871D;
				line-height: 40rpx;
				margin-bottom: 4rpx;
			}
			.text{
				font-size: 28rpx;
				font-weight: 400;
				color: #3B3535;
				line-height: 44rpx;
			}
		}
	}
}
.box2{
	padding: 32rpx 0 36rpx;
	width: 100%;
	background: #FFFFFF;
	// box-shadow: 0rpx 4rpx 8rpx 0rpx #ECE4E4;
	display: flex;
	justify-content: center;
	align-items: center;
	margin-bottom: 14rpx;
	.item{
		margin:  0 23rpx;
		text-align: center;
		image{
			display: block;
			margin: auto;
			width: 76rpx;
			height: 76rpx;
			margin-bottom: 16rpx;
		}
		text{
			font-size: 28rpx;
			font-weight: 400;
			color: #3B3535;
			line-height: 44rpx;
		}
	}
}
.box3{
	background-color: #FFFFFF;
	padding: 0 32rpx;
	.title{
		font-size: 28rpx;
		font-weight: 400;
		color: #3B3535;
		line-height: 44rpx;
		padding: 20rpx 0;
		border-bottom: 2rpx solid rgba(0,0,0,0.08);
	}
	.list{
		padding-bottom: 100rpx;
		.item{
			border-bottom: 2rpx solid rgba(0,0,0,0.08);
			padding:24rpx 14rpx 24rpx 34rpx  ;
			.first-line{
				display: flex;
				justify-content: space-between;
				margin-bottom: 24rpx;
				.left{
					display: flex;
					align-items: center;
					.touxiang-box{
						position: relative;
						vertical-align: middle;
						margin-right: 12rpx;
						.img{
							display: inline-block;
							width: 64rpx;
							height: 64rpx;
							border-radius: 50%;
						}
						.shu{
							text-align: center;
							position: absolute;
							top: -4rpx;
							right: -6rpx;
							display: inline-block;
							width: 32rpx;
							height: 32rpx;
							background: #FF3B30;
							border-radius: 50%;
							
							font-size: 22rpx;
							font-weight: 500;
							color: #FFFFFF;
							line-height: 32rpx;
						}
					}
					.name{
						font-size: 28rpx;
						font-weight: 500;
						color: #0D0E15;
						line-height: 40rpx;
						vertical-align: middle;
						margin-right: 20rpx;
						width: 104rpx;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
					.pet-info{
						font-size: 24rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #959595;
						line-height: 34rpx;
						vertical-align: middle;
					}
				}
				.right{
					display: flex;
					align-items: center;
					.wenzhenleixing{
						padding: 10rpx 20rpx;
						font-size: 28rpx;
						font-family: PingFangSC-Regular, PingFang SC;
						font-weight: 400;
						color: #FA6400;
						line-height: 40rpx;
						border-radius: 30rpx;
						border: 2rpx solid #FF9230;
					}
					.state{
						margin-left: 24rpx;
						font-size: 28rpx;
						font-weight: 400;
						color: #ED0B0D;
						line-height: 40rpx;
					}
				}
			} 
		}
		.second-line{
			display: flex;
			justify-content: space-between;
			align-items: center;
			.wenti{
				width: 450rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #959595;
				line-height: 40rpx;
				
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
			.time{
				font-size: 26rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #979797;
				line-height: 36rpx;
			}
		}
	}

	.zanwuyuyue{
		padding-top: 200rpx;
		text-align: center;
		image{
			display: block;
			margin: auto;
			width: 120rpx;
			height: 120rpx;
			margin-bottom: 50rpx;
		}
		text{
			font-size: 28rpx;
			font-weight: 400;
			line-height: 40rpx;
			color: #959595;
		}
	}
	
}
</style>
